<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Manage_department extends CI_Controller {

    public $session_data;

    public function __construct()
    {
        parent::__construct();
        //Not Completed
        $this->load->model('info_model');
        $this->load->model('manage_department_model');
        $this->load->helper("url");
        $this->load->library("pagination");

        $this->session_data = $this->session->userdata('logged_in');
    }

    public function manage_dept()
    {

        if (!$this->session->userdata('logged_in'))
            redirect('welcome/home','refresh');

        $session_data = $this->session_data['user_id'];

        $this->check_user($session_data['user_id']);

        $data['owner_id'] = $this->session_data['user_id'];

        $this->load->helper('form');
        $this->load->library("pagination");
        $data['title'] = "Manage Department";

        $session_data = $this->session->userdata('logged_in');


        $config["base_url"] = base_url()."index.php/manage_department/manage_dept";
        $config["total_rows"] = $this->manage_department_model->get_user_count();
        $config["per_page"] = 10;
        $config["uri_segment"] = 3;

        $this->pagination->initialize($config);

        $page = ($this->uri->segment(3))? $this->uri->segment(3) : 0;

        $result = $this->info_model->check_acc($session_data['user_id']);
        $data['user_dept'] = $this->manage_department_model->display_department($config["per_page"], $page, $result->user_id);

        //echo var_dump($data['user_dept']);

        if (empty($data['user_dept'])){

            $data['user_dept'] = $this->manage_department_model->display_department_no_dean($config["per_page"], $page, $result->user_id);

        }

        //echo var_dump($data['user_dept']);

        $data["links"] = $this->pagination->create_links();

        $data['college'] = $this->manage_department_model->display_user_college();

        $data['type'] = $this->manage_department_model->display_user_type();

        $session_data = $this->session->userdata('logged_in');
        $data['typeh'] = $session_data['type_no'];

        $this->load->view('template/header', $data);
        $this->load->view('pages/manage_department',$data);
        $this->load->view('template/footer');

    }

    public function add_college()
    {
        //THERE IS NO FUNCTION THAT DETECTS WETHERE THE INPUT USERNAME IS DUPLICATE OR NOT

        if (!$this->session->userdata('logged_in'))
            redirect('welcome/home','refresh');

        $this->load->library('form_validation');

        $this->form_validation->set_rules('college_name', 'College', 'trim|required|xss_clean');
        $this->form_validation->set_rules('user', 'Username', 'trim|required|xss_clean');
        $this->form_validation->set_rules('pass', 'Password', 'trim|xss_clean');
        $this->form_validation->set_rules('fname', 'First Name', 'trim|required|xss_clean');
        $this->form_validation->set_rules('middle_initial', 'Middle Initial', 'trim|required|xss_clean');
        $this->form_validation->set_rules('lname', 'Last Name', 'trim|required|xss_clean');

        if ($this->form_validation->run() === FALSE)
        {

            redirect('manage_department/manage_dept','refresh');

        }

        else
        {

            $col_name = $this->input->post("college_name");

            $check_dept = $this->manage_department_model->get_dept_id($col_name);

            if(count($check_dept) >= 1)
                redirect('manage_department/manage_dept','refresh');

            //echo var_dump(count($check_dept));

            $input = array(
                "coll_name" => $col_name
            );

            $this->manage_department_model->insert_dept($input);
            $coll = $this->manage_department_model->get_dept_id($col_name);
            $coll_id = intval($coll[0]['coll_id']);

            //Get id from the college name given
            //echo var_dump($coll_id);

            $pass = $this->input->post('pass');

            if(empty($pass)){

                $new = array(
                    "user_id" => $this->input->post('user'),
                    "type_no" => "1",
                    "coll_id" => $coll_id
                );

            }
            else{

                $new = array(
                    "user_id" => $this->input->post('user'),
                    "password" => $this->input->post('pass'),
                    "type_no" => "1",
                    "coll_id" => $coll_id
                );

            }

            $new2 = array(
                "user_id" => $this->input->post('user'),
                "fname" => $this->input->post('fname'),
                "middle_initial" => $this->input->post('middle_initial'),
                "lname" => $this->input->post('lname')
            );

            $new3 = array(
                "user_id" => $this->input->post('user')
            );

            $this->manage_department_model->insert_data($new, $new2, $new3);

            redirect('manage_department/manage_dept','refresh');

        }

    }

    public function add_dean()
    {
        //THERE IS NO FUNCTION THAT DETECTS WHETHER THE INPUT USERNAME IS DUPLICATE OR NOT

        if (!$this->session->userdata('logged_in'))
            redirect('welcome/home','refresh');

        $this->load->library('form_validation');

        $this->form_validation->set_rules('dean_user', 'Username', 'trim|required|xss_clean');
        $this->form_validation->set_rules('dean_pass', 'Password', 'trim|xss_clean');
        $this->form_validation->set_rules('dean_fname', 'First Name', 'trim|required|xss_clean');
        $this->form_validation->set_rules('dean_middle_initial', 'Middle Initial', 'trim|required|xss_clean');
        $this->form_validation->set_rules('dean_lname', 'Last Name', 'trim|required|xss_clean');

        if ($this->form_validation->run() === FALSE)
        {

            redirect('manage_department/manage_dept','refresh');

        }

        else
        {

            $col_id = $this->input->post("college_id");

            $check = $this->manage_department_model->check_acc($this->input->post('dean_user'));

            if(count($check) == 1)
                redirect('manage_department/manage_dept','refresh');

            $new = array(
                "user_id" => $this->input->post('dean_user'),
                "password" => $this->input->post('dean_pass'),
                "type_no" => "1",
                "coll_id" => $col_id
            );

            $new2 = array(
                "user_id" => $this->input->post('dean_user'),
                "fname" => $this->input->post('dean_fname'),
                "middle_initial" => $this->input->post('dean_middle_initial'),
                "lname" => $this->input->post('dean_lname')
            );

            $new3 = array(
                "user_id" => $this->input->post('dean_user')
            );

            $this->manage_department_model->insert_data($new, $new2, $new3);

            redirect('manage_department/manage_dept','refresh');

        }

    }

    public function edit_college()
    {

        if (!$this->session->userdata('logged_in'))
            redirect('welcome/home','refresh');

        $coll_id = $this->input->post("college_id");

        $result = $this->manage_department_model->check_college($coll_id);

        $this->load->library('form_validation');

        $this->form_validation->set_rules('edit_college', 'College Name', 'trim|required|xss_clean');

        if ($this->form_validation->run() === FALSE)
        {

            redirect('manage_department/manage_dept','refresh');

        }

        else
        {
            $coll_name = $this->input->post('edit_college');
            if($coll_name != $result[0]['coll_name'])
                redirect('manage_department/manage_dept','refresh');

            //$check = $this->manage_department_model->check_acc($this->input->post('dean_user'));

            //if(count($check) == 1)
                //redirect('manage_department/manage_dept','refresh');

            $new = array(
                "coll_name" => $this->input->post('edit_college'),
            );

            //echo var_dump($coll_name);
            //echo var_dump($result[0]['coll_name']);
            //echo var_dump($coll_name != $result[0]['coll_name']);

            $this->manage_department_model->edit_college($new,$coll_id);

            //redirect('manage_department/manage_dept','refresh');

        }

    }

    public function delete_dept($dept){

        //This function should check if the user is one level higher than the chosen user so that they can delete them.
        //Can't yet delete the files but can delete the photos

        if (!$this->session->userdata('logged_in'))
            redirect('welcome/home','refresh');

        $var = $this->manage_department_model->get_pic_link($dept);
        echo var_dump($var);

        $path = "assets/images/profile/";

        foreach($var as $item){
            //echo var_dump(base_url() . $path . $item['img_name']);
            //echo $item['img_name'] == "default.png";

            if(file_exists($path . $item['img_name'])){
                if(!($item['img_name'] == "default.png")){
                    unlink($path . $item['img_name']);
                }
            }
            //echo var_dump(file_exists($path . $item['img_name']));
        }

        $this->manage_department_model->delete_dept($dept);
        $this->manage_department_model->delete_dept($dept);

        redirect('manage_department/manage_dept','refresh');

    }

    public function check_user(){

        $session_data = $this->session->userdata('logged_in');
        $user_id = $session_data['user_id'];

        $result = $this->info_model->check_user($user_id);

        $type = $session_data['type_no'];
        $user_id = $session_data['user_id'];
        $coll_id = $session_data['coll_id'];

        if( ($type != 0 ) )
            redirect('announcement/view', 'refresh');

        if(empty($result))
            redirect('announcement/view', 'refresh');

        //echo var_dump($session_data);
        //echo var_dump($user);
        //echo var_dump($result['coll_id'] == $session_data['coll_id']);

    }

    public function logout()
    {

        $this->session->unset_userdata('logged_in');
        redirect('welcome/home', 'refresh');

    }

}

/* End of file manage_account.php */
/* Location: ./application/controllers/manage_account.php */